<?php


namespace App\Business;


use Illuminate\Support\Facades\Redis;

class ImageController
{

    /**
     * ImageController constructor.
     * 私有的构造方法
     */
    private function __construct()
    {

    }



    /**
     * 静态的克隆方法
     */
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }



    /**
     * @param $image
     * @param $data
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|string
     */
    static public function image($image,$data)
    {
        $disk = \Storage::disk('qiniu'); //使用七牛云上传

        $time = date('Y-m-d');

        $filename = $disk->put($time, request()->file('image'));//上传

        $res = ImageController::images($image);

        if(json_decode($res,true)['conclusion'] == '合规')
        {
            if(!$filename) {
                echo "上传失败";
            }

//            获取图片链接
            $img_url = $disk->getDriver()->downloadUrl($filename); //获取下载链接
            $data['images'] = $img_url;

//            将数据进行缓存操作
            Redis::sadd('show',json_encode($data));

//            有序集合存储，进行排序操作
            Redis::zadd('list',$data['numberState'],$data['name']);

//            执行添加操作
            Stan::user()->add($data);
            return redirect('page');
        }
        else{
            return '图片不合规';
        }
    }



    /**
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    static public function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        // 初始化curl
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $postUrl);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        // 运行curl
        $data = curl_exec($curl);
        curl_close($curl);

        return $data;
    }



    /**
     * @return mixed
     */
    static public function tokens()
    {
        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type']       = 'client_credentials';
        $post_data['client_id']      = 'INjM2HZbisZgMzazxBRThA44';
        $post_data['client_secret'] = 'ZPGu1Y7IDfxbSiE4Vmu5bXtmp39938Ft';
        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $res = ImageController::request_post($url, $post_data);

//        dd(json_decode($res));
        return json_decode($res,true)['access_token'];
    }



    /**
     * @param $image
     * @return bool|string
     */
    static public function images($image)
    {
        $token = ImageController::tokens();
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
        $img = file_get_contents($image);
        $img = base64_encode($img);
        $bodys = array(
            'image' => $img
        );

//        进行将监测后的值进行返回操作
        $res = ImageController::request_post($url, $bodys);

        return $res;
    }
}
